﻿using UnityEngine;
using System.Collections;
using System;



public class ApplovinAds:BaseAds<ApplovinAds>
{
	public static bool showBanner = false;
	static Action finishtodo = null;
	static Action loadVideo = null;
	
	public ApplovinAds ()
	{
		
	}
	
	public override void Init (params string[] key)
	{
		Debug.Log ("YXC" + "Applovin Init key" + key [0]);
		AppLovin.SetSdkKey (key [0]);
		AppLovin.InitializeSdk ();
		AppLovin.SetUnityAdListener (key [1]);
		GetIncentivizedVideo ();
		GetInterstitial ();
	}
	
	#region  展示banner 视屏 插屏
	
	public override void PlayBanner (bool show)
	{
		showBanner = show;
		if (show) {
			
		} else {
			
		}
		//      AdsYuMiBannerUnity.setAdsYuMiViewHidden (!show);
	}
	
	public override void ShowIncentVideo (Action ac = null)
	{
		
		if (ac != null) {
			finishtodo = ac;
		} else {
			finishtodo = null;
		}
		loadVideo = GetIncentivizedVideo;
		ShowRewardedInterstitial ();
	}
	
	public override void ShowInterVideo (Action ac = null)
	{
		
		if (ac != null) {
			finishtodo = ac;
		} else {
			finishtodo = null;
		}
		loadVideo = GetInterstitial;
		ShowInterstitial ();
	}
	
	public override void ShowInterstitial ()
	{
		Debug.Log ("YXC" + "展示Applovin插屏视频");
		AppLovin.ShowInterstitial ();
	}
	
	public override void ShowRewardedInterstitial ()
	{
		Debug.Log ("YXC" + "展示Applovin激励视频");
		AppLovin.ShowRewardedInterstitial ();
	}
	
	#endregion
	
	public override void AdsCallback (string ev)
	{
		Debug.Log ("YXC" + "  回调信息  " + ev);
		if (ev.Contains ("REWARDAPPROVEDINFO")) {
			
			// The format would be "REWARDAPPROVEDINFO|AMOUNT|CURRENCY" so "REWARDAPPROVEDINFO|10|Coins" for example
			
			// Split the string based on the delimeter
			string[] split = ev.Split ('|');
			
			// Pull out the currency amount
			double amount = double.Parse (split [1]);
			
			// Pull out the currency name
			string currencyName = split [2];
			
			// Do something with the values from above.  For example, grant the coins to the user.
			Debug.Log ("YXC" + "  applovin视频开始播放  ");
		} else if (ev.Contains ("LOADEDREWARDED")) {
			// A rewarded video was successfully loaded.
			Debug.Log ("YXC" + "load applovin reward video success");
		} else if (ev.Contains ("LOADEDINTER")) {
			Debug.Log ("YXC" + "load applovin inter video success");
		} else if (ev.Contains ("LOADREWARDEDFAILED")) {
			// A rewarded video failed to load.
			Debug.Log ("YXC" + "load applovin  reward video fail go to load again");
			GetIncentivizedVideo ();
		} else if (ev.Contains ("LOADINTERFAILED")) {
			Debug.Log ("YXC" + "load applovin inter video fail go to load again");
			GetInterstitial ();
		} else if (ev.Contains ("HIDDENREWARDED") || ev.Contains ("HIDDENINTER")) {
			// A rewarded video was closed.  Preload the next rewarded video.
			Debug.Log ("YXC" + "applovin视频播放完成，现在取下次视频");
			if (finishtodo != null)
				finishtodo ();
			loadVideo ();
		}
	}
	
	#region 获取视频和插屏
	
	public override void GetIncentivizedVideo ()
	{
		Debug.Log ("YXC" + "获取Applovin视频");
		AppLovin.LoadRewardedInterstitial ();
		
	}
	
	public override void GetInterstitial ()
	{
		Debug.Log ("YXC" + "获取Applovin 插频");
		AppLovin.PreloadInterstitial ();
	}
	
	#endregion
	
	#region 判断视频和插屏是否加载好
	
	public override bool IsIncentivizedAvailable ()
	{
		
		if (AppLovin.IsIncentInterstitialReady ()) {
			Debug.Log ("YXC" + "applovin 视频加载好啦");
			return true;
		} else {
			Debug.Log ("YXC" + "applovin 视频没加载好");
			GetIncentivizedVideo ();
		}
		
		return false;
	}
	
	public override bool IsInterstitialAvailable ()
	{
		if (AppLovin.HasPreloadedInterstitial ()) {
			Debug.Log ("YXC" + "applovin 插频加载好啦");
			return true;
		} else {
			Debug.Log ("YXC" + "applovin 插频没加载好");
			GetInterstitial ();
			return false;
		}
	}
	
	#endregion
	
}
